package com.bt.lms.ui.page.school_education;


import com.bt.lms.ui.action.school_education.PagingQueryAction;
import com.bt.lms.ui.page.sys.style.BtnStyle;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;

/**
 * 学籍信息主列表界面
 */
public class PageStudentList extends VBox {
    /**
     * 界面宽度
     */
    private double width = 1200;

    /**
     * 条件语句
     */
    private ComboBox<String> conditionCB;

    /**
     * 条件名称
     */
    private TextField conditionTF;

    /**
     * 学员编号
     */
    private TextField codeTF;

    /**
     * 姓名
     */
    private TextField nameTF;

    /**
     * 学籍状态
     */
    private ComboBox<String> statusCB;

    /**
     * 搜索按钮
     */
    private Button searchBtn;

    /**
     * tableview表格
     */
    private TableView dataTV;

    /**
     * 当前页
     */
    private int currentPage = 1;
    /**
     * 每页显示条数
     */
    private int pageCount = 10;

    /**
     * 服务端返回来的总数据
     */
    private long total;

    /**
     * 分页查询跳转输入框
     */
    private TextField textField;


    public PageStudentList() {
        Insets insets = new Insets(10);
        this.setPadding(insets);
        this.setSpacing(10);
        this.setPrefWidth(width);
        //初始化标题索引
        initTitleIndex();

        Label studentList = new Label("学员名单");
        studentList.setFont(Font.font("圆体", FontWeight.BLACK, 30));
        this.getChildren().add(studentList);

        //初始化条件列表盒子
        initTopBox();

        //获取数据内容
        PagingQueryAction pagingQueryAction = new PagingQueryAction();
        //获取显现在界面上的数据tableview
        String data = pagingQueryAction.getData(this.currentPage);
        pagingQueryAction.renderData(data);
        //获取总记录数
        this.total = pagingQueryAction.getTotal();
        this.dataTV = pagingQueryAction.getDataTV();
        this.getChildren().add(this.dataTV);

        //初始化分页查询盒子
        initPagingQueryBox();


    }


    /**
     * 初始化分页查询盒子
     */
    private void initPagingQueryBox() {

        HBox hBox = new HBox();
        hBox.setPrefWidth(1200);
        hBox.setSpacing(10);
        Insets insets = new Insets(10);
        hBox.setPadding(insets);
        hBox.setAlignment(Pos.CENTER_RIGHT);

        //事件
        PagingQueryAction pagingQueryAction = new PagingQueryAction();
        pagingQueryAction.setBox(this);
        Label homePageLab = new Label("<<首页");
        homePageLab.setCursor(Cursor.HAND);
        homePageLab.setOnMouseClicked(pagingQueryAction);
        homePageLab.setStyle("-fx-text-fill: #52b82d");
        Label previousPageLab = new Label("<<上一页");
        previousPageLab.setOnMouseClicked(pagingQueryAction);
        previousPageLab.setCursor(Cursor.HAND);
        previousPageLab.setStyle("-fx-text-fill: #52b82d");

        hBox.getChildren().addAll(homePageLab, previousPageLab);
        //向上取整 一共有几页数据
        int ceil = (int) Math.ceil((double) this.total / this.pageCount);
        int o = 0;
        if (ceil <= 5){
            for (int i = 1;i <= ceil ; i++){
                o ++;
                Button btn = new Button(String.valueOf(o));
                btn.setCursor(Cursor.HAND);
                btn.setOnAction(pagingQueryAction);
                hBox.getChildren().add(btn);
                //鼠标移入事件
                btn.setOnMouseEntered(event -> {
                    btn.setStyle("-fx-background-color: #52b82d");
                });
                //鼠标移出事件
                btn.setOnMouseExited(event -> {
                    btn.setStyle("-fx-background-color: #757070");
                });
            }
        }else {
            for (int i = 1;i <= 5 ; i++){
                o ++;
                Button btn = new Button(String.valueOf(o));
                btn.setCursor(Cursor.HAND);
                btn.setOnAction(pagingQueryAction);
                hBox.getChildren().add(btn);
                //鼠标移入事件
                btn.setOnMouseEntered(event -> {
                    btn.setStyle("-fx-background-color: #52b82d");
                });
                //鼠标移出事件
                btn.setOnMouseExited(event -> {
                    btn.setStyle("-fx-background-color: #efeded");
                });
            }
        }

        Label nextPageLab = new Label("下一页>>");
        nextPageLab.setOnMouseClicked(pagingQueryAction);
        nextPageLab.setCursor(Cursor.HAND);
        nextPageLab.setStyle("-fx-text-fill: #52b82d");
        Label trailerPageLab = new Label("尾页>>");
        trailerPageLab.setOnMouseClicked(pagingQueryAction);
        trailerPageLab.setCursor(Cursor.HAND);
        trailerPageLab.setStyle("-fx-text-fill: #52b82d");
        Label TotalRecordsLab = new Label("总记录数");
        Label figureLab = new Label(String.valueOf(this.total));
        figureLab.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setHeaderText(null);
                String text = "当前有" + total + "条数据哦！";
                alert.setContentText(text);
                alert.show();
            }
        });
        figureLab.setCursor(Cursor.HAND);
        figureLab.setStyle("-fx-text-fill: red");
        Label stripLab = new Label("条");
        this.textField = new TextField();
        this.textField.setPrefWidth(40);
        this.textField.setPrefHeight(10);

        Label tenLab = new Label("/" + ceil);
        Button GOBtn = new Button("GO");
        GOBtn.setCursor(Cursor.HAND);
        GOBtn.setOnAction(pagingQueryAction);
        hBox.getChildren().addAll( nextPageLab, trailerPageLab, TotalRecordsLab, figureLab, stripLab, this.textField, tenLab, GOBtn);
        this.getChildren().add(hBox);


    }

    /**
     * 初始化标题索引
     */
    private void initTitleIndex() {
        HBox hBox = new HBox();
        hBox.setPrefHeight(30);
        hBox.setAlignment(Pos.CENTER_LEFT);

        Label label = new Label("当前位置：🏠");
        Label label1 = new Label(">学籍管理");
        label1.setStyle("-fx-text-fill: #00ccff");
        Label label2 = new Label(">学籍信息管理");
        hBox.getChildren().addAll(label, label1, label2);


        this.getChildren().add(hBox);

    }


    /**
     * 初始化条件列表盒子
     */
    private void initTopBox() {
        VBox vBox = new VBox();
        vBox.setPadding(new Insets(10));
        HBox topBox = new HBox();
        Insets insets = new Insets(0, 30, 0, 0);
        topBox.setPadding(insets);
        topBox.setSpacing(20);
        vBox.setStyle("-fx-background-color: #cdc9c9");

        this.conditionCB = new ComboBox<>();
        this.conditionCB.getItems().add("按招生计划");
        this.conditionCB.getItems().add("按学习中心");
        this.conditionCB.getItems().add("按专业");
        this.conditionCB.setId("enrollComboBox");
        //设置默认显示
        this.conditionCB.getSelectionModel().select(0);

        Label dimQuery = new Label("模糊查询");
        this.conditionTF = new TextField();
        this.conditionTF.setPromptText("请输入招生计划名称");//灰色自显示提示
        this.conditionTF.setId("dimQueryTF");
        Label studentNumber = new Label("学员编号");
        this.codeTF = new TextField();
        this.codeTF.setId("studentNumberTF");
        topBox.getChildren().addAll(this.conditionCB, dimQuery, this.conditionTF, studentNumber, this.codeTF);
        vBox.getChildren().add(topBox);

        HBox two = new HBox();
        two.setPadding(new Insets(10, 30, 0, 0));
        two.setSpacing(20);
        Label name = new Label("姓名");
        this.nameTF = new TextField();
        this.nameTF.setId("nameTF");
        Label studentStatusState = new Label("学籍状态");
        this.statusCB = new ComboBox<>();
        this.statusCB.getItems().add("在籍");
        this.statusCB.getItems().add("未注册");
        this.statusCB.getItems().add("转出");
        this.statusCB.getItems().add("休学");
        this.statusCB.getItems().add("退学");
        this.statusCB.getItems().add("开除");
        this.statusCB.getItems().add("毕业");
        this.statusCB.getItems().add("异动中");
        this.statusCB.setId("studentStatusStateComBox");
        this.statusCB.getSelectionModel().select(0);
        //设置默认显示
        this.statusCB.getSelectionModel().select(0);
        Button searchBtn = new Button("搜索");
        BtnStyle.btn(searchBtn);
        searchBtn.setCursor(Cursor.HAND);
        two.getChildren().addAll(name, this.nameTF, studentStatusState, this.statusCB, searchBtn);
        vBox.getChildren().add(two);

        //搜索按钮事件
        PagingQueryAction pagingQueryAction = new PagingQueryAction();
        pagingQueryAction.setvBox(vBox);
        pagingQueryAction.setBox(this);
        searchBtn.setOnAction(pagingQueryAction);

        this.getChildren().add(vBox);

        this.conditionCB.setOnAction(event -> {
            if (this.conditionCB.getSelectionModel().isSelected(0)) {
                this.conditionCB.setPromptText("请输入招生计划名称");//灰色自显示提示
            }
            if (this.conditionCB.getSelectionModel().isSelected(1)) {
                this.conditionTF.setPromptText("请输入学习中心名称");//灰色自显示提示
            }
            if (this.conditionCB.getSelectionModel().isSelected(2)) {
                this.conditionTF.setPromptText("请输入专业名称");//灰色自显示提示
            }
        });
    }

    public ComboBox<String> getConditionCB() {
        return conditionCB;
    }

    public void setConditionCB(ComboBox<String> conditionCB) {
        this.conditionCB = conditionCB;
    }

    public TextField getConditionTF() {
        return conditionTF;
    }

    public void setConditionTF(TextField conditionTF) {
        this.conditionTF = conditionTF;
    }

    public TextField getCodeTF() {
        return codeTF;
    }

    public void setCodeTF(TextField codeTF) {
        this.codeTF = codeTF;
    }

    public TextField getNameTF() {
        return nameTF;
    }

    public void setNameTF(TextField nameTF) {
        this.nameTF = nameTF;
    }

    public ComboBox<String> getStatusCB() {
        return statusCB;
    }

    public void setStatusCB(ComboBox<String> statusCB) {
        this.statusCB = statusCB;
    }

    public Button getSearchBtn() {
        return searchBtn;
    }

    public void setSearchBtn(Button searchBtn) {
        this.searchBtn = searchBtn;
    }

    public TableView getDataTV() {
        return dataTV;
    }

    public void setDataTV(TableView dataTV) {
        this.dataTV = dataTV;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageCount() {
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public TextField getTextField() {
        return textField;
    }

    public void setTextField(TextField textField) {
        this.textField = textField;
    }

}
